package com.jymj.controller;


import com.jymj.entity.zjd.ApplyFileEntity;
import com.jymj.service.ApplyFileService;
import com.jymj.utils.FileUtile.FileUploadEntity;
import com.jymj.utils.FileUtile.FileUtile;
import com.jymj.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * Created by ljj on 2018/9/12.
 */
@RestController
@RequestMapping("/applyFile")
public class ApplyFileController extends AbstractController {
    @Autowired
    private ApplyFileService applyFileService;

    private final String APPLE_SAVE = "ApplyFile.dirPath";


    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("uploadFile")
    public R uploadFile(MultipartFile file,Integer cltype){
        FileUtile fileUtile = new FileUtile();
        FileUploadEntity fileUploadEntity = fileUtile.initFileUtile(APPLE_SAVE);
        fileUploadEntity= FileUtile.fileUpload(file, fileUploadEntity);
        ApplyFileEntity applyFileEntity = applyFileService.save(fileUploadEntity, cltype);
        return R.ok().put("data",applyFileEntity);
    }

    /**
     * 删除图片
     * @param ids
     * @return
     */
    @PostMapping("/deleteFile")
    public R deleteFile(@RequestBody Integer... ids){
        for(int i=0;i<ids.length;i++){
            ApplyFileEntity applyFileEntity = applyFileService.queryObject(ids[i]);
            if(applyFileEntity!=null && applyFileEntity.getApplyEntity()!=null
                    && applyFileEntity.getApplyEntity().getSptype()!=null){
                Integer sptype = applyFileEntity.getApplyEntity().getSptype();
                //审批通过不能删除文件  其他状态可以删
                if(sptype==3){
                    return R.error("审批通过，文件不可删除！");
                }
                applyFileService.delete(ids[i]);
                FileUploadEntity fileUploadEntity = new FileUtile().initFileUtile(APPLE_SAVE);
                FileUtile.fileDelete(applyFileEntity.getUrl(),fileUploadEntity);
            }
        }
        return R.ok("删除成功！");
    }

}
